//
//  ViewController.m
//  CallPolice
//
//  Created by mac on 2017/12/5.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "TableViewDelegate.h"
#import "MsgTableViewCell.h"
#import "WXDataBase.h"
#import "MessageModel.h"
#import "ConfigureHeader.h"
#define kDevice_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
@interface ViewController (){
    NSArray *messages;

}
@property (weak, nonatomic) IBOutlet UITableView *msgTableView;
@property (strong, nonatomic) TableViewDelegate *tableViewDelegate;
@property (nonatomic,strong)   NSMutableArray *mutableArray;
@property (nonatomic,strong) NSDateFormatter *formatter;

@end

@implementation ViewController
- (NSDateFormatter *)formatter{
    if (!_formatter) {
        _formatter =[[NSDateFormatter alloc]init];
        [_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    }
    return _formatter;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(pushRemote:) name:PUSH_REMOTE object:nil];
    
    // Do any additional setup after loading the view, typically from a nib.
//    formatter =[[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.title = self.userName;
//    if (@available(iOS 8 , *)) {
//        self.automaticallyAdjustsScrollViewInsets =NO;
//    }
//    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//
//    NSLog(@"%f",statusBar.frame.size.height);
//    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
//        statusBar.backgroundColor = [UIColor colorWithRed:17.0f/255.0 green:129.0f/255.0 blue:209.0f/255.0 alpha:1];
//    }
    [self setUpTableView];

}
- (IBAction)add:(id)sender {
    [self pushRemote:nil];
}
- (void)setUpTableView{
    self.tableViewDelegate = [[TableViewDelegate alloc]initWithTableView:self.msgTableView listCell:NSStringFromClass([MsgTableViewCell class]) itemCount:0];
    self.msgTableView.layer.cornerRadius = 7;
    self.msgTableView.layer.borderWidth = 0;
    self.msgTableView.layer.masksToBounds = YES;
    self.tableViewDelegate.className = NSStringFromClass([MsgTableViewCell class]);
    _mutableArray = [[NSMutableArray alloc]init];
    self.tableViewDelegate.itemArrayItems = _mutableArray;
    __weak typeof(self)weakSelf = self;
    self.tableViewDelegate.block = ^(NSIndexPath *path, UITableViewCell *cell) {
         MessageModel *model =[weakSelf.tableViewDelegate.itemArrayItems objectAtIndex:path.row];
        MsgTableViewCell *msgCell = (MsgTableViewCell *)cell;
        msgCell.contentLabel.text = model.content;
        msgCell.dateLabel.text = model.createTime;
    };
    self.tableViewDelegate.selectBlock = ^(NSIndexPath *indexPath) {
        
    };
    __block  BOOL isTop =YES;
   __block CGFloat statusBarHeight = kDevice_Is_iPhoneX?44.0f:20.0f;
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(weakSelf.view.frame), statusBarHeight)];
    aView.backgroundColor = [UIColor colorWithRed:17/255.0 green:129/255.0 blue:209/255.0 alpha:1];
    
    self.tableViewDelegate.scrollviewBlock = ^(CGFloat height) {
        if (!weakSelf.mutableArray.count) {
            return;
        }
        if (height>0.0f) {
            if (isTop) {
                [UIView animateWithDuration:0.15f animations:^{
                    weakSelf.navigationController.navigationBar.frame = CGRectMake(0, (-44.0f-statusBarHeight), weakSelf.view.bounds.size.width, 44);
                    [weakSelf.view addSubview:aView];

                }];
                isTop =NO;
            }
        }
        if (height<0) {
            
            if (!isTop)
            {
                [UIView animateWithDuration:0.15f animations:^{
                    weakSelf.navigationController.navigationBar.frame = CGRectMake(0, statusBarHeight, weakSelf.view.bounds.size.width, 44);
                    [aView removeFromSuperview];

                }];
                isTop =YES;
                
            }
        }
    };
}
- (void)pushRemote:(NSNotification*)center{
    MessageModel *model =[[MessageModel alloc]init];
    model.localID = [NSDate date];
    NSDate *date =[NSDate dateWithTimeIntervalSince1970:[[center.userInfo valueForKeyPath:@"date"] integerValue]];
    model.createTime = [self.formatter stringFromDate:date];
    model.content = [center.userInfo valueForKeyPath:@"content"];
    [self.mutableArray insertObject:model atIndex:0];
    [self.tableViewDelegate reloadMyData];
    
    
}
- (IBAction)cleanAction:(UIButton*)sender {
    self.tableViewDelegate.scrollviewBlock(-1.0f);
    [_mutableArray removeAllObjects];
    [_tableViewDelegate reloadMyData];
}
- (IBAction)tapAction{
    WCTOneRow *row =[[WXDataBase ShareDataBaseInstance] getOneRowOnResults:{MessageModel.localID.count()} fromTable:TABLE_NAME];
    [self.mutableArray removeAllObjects];
    NSNumber* total = (NSNumber *)row[0];
    if ([total intValue]<100) {
        messages = [[WXDataBase ShareDataBaseInstance]getAllObjectsOfClass:MessageModel.class fromTable:TABLE_NAME];
     
    }else{
        messages = [[WXDataBase ShareDataBaseInstance]getObjectsOfClass:MessageModel.class fromTable:TABLE_NAME limit:100 offset:[total intValue]-100];
    }
    _mutableArray = [[[messages  reverseObjectEnumerator]allObjects] mutableCopy];
    self.tableViewDelegate.itemArrayItems = _mutableArray;
    [self.tableViewDelegate reloadMyData];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
 
    
 
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
